<?php
class WebsitesController extends AppController {

	var $name = 'Websites';
	var $helpers = array('Html', 'Form');
        var $uses = array("Website","User");
            function beforeFilter() {
                $this->set('page_title','Website Management');
                parent::beforeFilter();
                        //$this->Auth->allow('*');
                }
        
	function index() {
		$this->Website->recursive = 0;
                $this->paginate['Website']['conditions']['Website.user_id'] = $this->Auth->User('id');
		$this->set('websites', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Website.', true),'error');
			$this->redirect(array('controller'=>'users','action'=>'index'));
		}
		$this->set('website', $this->Website->read(null, $id));
	}
        function admin_index($id=null) {
         if (!$id) {
            $this->Session->setFlash(__('Invalid id for User', true), 'error');
            $this->redirect(array('controller'=>'users','action' => 'index'));
        }
		$this->Website->recursive = 0;
                if($id !=null){
                    $this->set('select_user',true);
                     $this->paginate['Website']['conditions']['Website.user_id'] = $id;
                }
                $this->Website->bindModel(array('belongsTo' => array('User' => array('foreignKey' => 0, 'conditions' => 'Website.user_id = User.id'))), false);
                $this->set('websites', $this->paginate());
                $this->set("user_id",$id);
	}
	function admin_add($id=null) {
         if (!$id) {
            $this->Session->setFlash(__('Invalid id for User', true), 'error');
            $this->redirect(array('controller'=>'users','action' => 'index'));
        }
		if (!empty($this->data)) {
			$this->Website->create();
			if ($this->Website->save($this->data)) {
				$this->Session->setFlash(__('The Website has been saved', true),'success');
				$this->redirect(array('action'=>'index/'.$id));
			} else {
				$this->Session->setFlash(__('The Website could not be saved. Please, try again.', true), 'success');
			}
		}
		$this->set('id',$id);
                
	}

	function admin_edit($user_id=null,$id = null) {
         if (!$user_id) {
            $this->Session->setFlash(__('Invalid id for User', true), 'error');
            $this->redirect(array('controller'=>'users','action' => 'index'));
        }            
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Website', true), 'error');
			$this->redirect(array('controller'=>'users','action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Website->save($this->data)) {
				$this->Session->setFlash(__('The Website has been saved', true),'success');
				$this->redirect(array('action'=>'index/'.$user_id));
			} else {
				$this->Session->setFlash(__('The Website could not be saved. Please, try again.', true), 'error');
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Website->read(null, $id);
		}
		$this->set('id',$id);
                $this->set('user_id',$user_id);
	}

	function admin_delete($user_id=null,$id = null) {
         if (!$user_id) {
            $this->Session->setFlash(__('Invalid id for User', true), 'error');
            $this->redirect(array('controller'=>'users','action' => 'index'));
        }              
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Website', true), 'error');
			$this->redirect(array('controller'=>'users','action'=>'index'));
		}
		if ($this->Website->delete($id)) {
			$this->Session->setFlash(__('Website deleted', true), 'success');
			$this->redirect(array('action'=>'index/'.$user_id));
		}
	}

}
?>